
package me.zhengjie.service.impl;

import me.zhengjie.domain.AppWorkGps;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.repository.AppWorkGpsRepository;
import me.zhengjie.service.AppWorkGpsService;
import me.zhengjie.service.dto.AppWorkGpsDto;
import me.zhengjie.service.dto.AppWorkGpsQueryCriteria;
import me.zhengjie.service.mapstruct.AppWorkGpsMapper;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author IQW
* @date 2022-07-03
**/
@Service
@RequiredArgsConstructor
public class AppWorkGpsServiceImpl implements AppWorkGpsService {

    private final AppWorkGpsRepository appWorkGpsRepository;
    private final AppWorkGpsMapper appWorkGpsMapper;

    @Override
    public Map<String,Object> queryAll(AppWorkGpsQueryCriteria criteria, Pageable pageable){
        Page<AppWorkGps> page = appWorkGpsRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(appWorkGpsMapper::toDto));
    }

    @Override
    public List<AppWorkGpsDto> queryAll(AppWorkGpsQueryCriteria criteria){
        //加个排序
        Sort sort = Sort.by(Sort.Direction.ASC, "id");
        return appWorkGpsMapper.toDto(appWorkGpsRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),sort));
    }

    @Override
    @Transactional
    public AppWorkGpsDto findById(Integer id) {
        AppWorkGps appWorkGps = appWorkGpsRepository.findById(id).orElseGet(AppWorkGps::new);
        ValidationUtil.isNull(appWorkGps.getId(),"AppWorkGps","id",id);
        return appWorkGpsMapper.toDto(appWorkGps);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AppWorkGpsDto create(AppWorkGps resources) {
        return appWorkGpsMapper.toDto(appWorkGpsRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(AppWorkGps resources) {
        AppWorkGps appWorkGps = appWorkGpsRepository.findById(resources.getId()).orElseGet(AppWorkGps::new);
        ValidationUtil.isNull( appWorkGps.getId(),"AppWorkGps","id",resources.getId());
        appWorkGps.copy(resources);
        appWorkGpsRepository.save(appWorkGps);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(Integer[] ids) {
        for (Integer workId : ids) {
            appWorkGpsRepository.deleteById(workId);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteByWorkId(Integer workId) {
        appWorkGpsRepository.deleteByWorkId(workId);
    }

    @Override
    public void download(List<AppWorkGpsDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (AppWorkGpsDto appWorkGps : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("时间戳", appWorkGps.getTime());
            map.put("经度", appWorkGps.getLon());
            map.put("纬度", appWorkGps.getLat());
            map.put("已行驶时长（秒）", appWorkGps.getWorkTime());
            map.put("已行驶里程（米）", appWorkGps.getWorkMile());
            map.put("状态", appWorkGps.getStatus());
            map.put("创建日期", appWorkGps.getCreateTime());
            map.put("更新日期", appWorkGps.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}